package com.tbsoft.ehr.action.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionContext;
import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.model.AttachFile;
import com.tbsoft.ehr.service.common.UploadFileService;
import com.tbsoft.ehr.util.ConfigProps;

/**
 * @desc 附件上传 Action
 * @author vastdream
 * 
 */
public class UploadFileAction extends BaseAction {
	private String tmpAttachPath = (String) ConfigProps.getContextProperty("attach.file.path")+File.separator+"tmp";
	private String storeAttachPath = (String) ConfigProps.getContextProperty("attach.file.path")+File.separator+"store";
	private UploadFileService uploadFileService;


	public synchronized void setUploadFileService(UploadFileService uploadFileService) {
		this.uploadFileService = uploadFileService;
	}

	private Log log = LogFactory.getLog(getClass());
	private static final long serialVersionUID = 1L;
	private String uploadFile;
	private String parentNodeId;
	public synchronized String getParentNodeId() {
		return parentNodeId;
	}

	public synchronized void setParentNodeId(String parentNodeId) {
		this.parentNodeId = parentNodeId;
	}

	public synchronized void setUploadFile(String uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String openWindow() {
		return "uploadview";
	}


	public String sendFile() throws IOException {
		Map<String, Object> map = ActionContext.getContext().getParameters();
		fileName = ((String[]) map.get("Filename"))[0].replaceAll("\\s", "");
		log.info("receive file:[" + fileName + "]" + uploadFile);
		String fileId=uploadFileService.receiveFile(fileName, uploadFile);
		setFileId(fileId);
		return  SUCCESS;
	}
	@JSON(serialize=false)
	public InputStream getFileInputStream() throws Exception {
		log.info("classpath:" + System.getProperty("java.class.path"));
		String path = tmpFile?tmpAttachPath:storeAttachPath;
		String fileName = path + File.separator + fileId;
		FileInputStream fis = new FileInputStream(fileName);
		return fis;
	}
	@JSON(serialize=false)
	public String getFile(){
		return "getFile";
	}
	private String fileName;
	private String fileId;
	private boolean tmpFile;
	@JSON(serialize=false)
	public synchronized boolean isTmpFile() {
		return tmpFile;
	}

	public synchronized void setTmpFile(boolean tmpFile) {
		this.tmpFile = tmpFile;
	}

	public synchronized String getFileId() {
		return fileId;
	}

	public synchronized void setFileId(String fileId) {
		this.fileId = fileId;
	}

	public synchronized String getFileName() {
		return fileName;
	}

	public synchronized void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	private String businessId;


	public synchronized String getBusinessId() {
		return businessId;
	}

	public synchronized void setBusinessId(String businessId) {
		this.businessId = businessId;
	}
	
	public String loadFileList(){
		attachFileList = uploadFileService.getAttachListByBusinessId(businessId);
		return SUCCESS;
	}
	private List<AttachFile> attachFileList;
	public synchronized List<AttachFile> getAttachFileList() {
		return attachFileList;
	}	
}
